package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.framework.BasePresenter;
import com.taomu.app.zongchouhuli.http.res.ResBankEntity;
import com.taomu.app.zongchouhuli.http.res.ResMyBankListEntity;
import com.taomu.app.zongchouhuli.moudle.mine.adpter.AccountBankAdp;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserAccountContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.UserAccountPresenter;
import com.taomu.app.zongchouhuli.moudle.mine.entity.AccountBankEntity;
import com.taomu.app.zongchouhuli.utils.ListViewUtils;
import com.taomu.app.zongchouhuli.weight.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class UserAccountActivity extends BaseActivity<UserAccountContract.Presenter> implements UserAccountContract.View {

    private ImageView mBackIm;

    private TextView mAddBankTv;

    private EmptyRecyclerView mRecy;

    private AccountBankAdp mBankAdp;

    private SmartRefreshLayout mRefreshLayout;

    private int ship = 0;

    private int take = 10;

    @Override
    protected void onResume() {
        super.onResume();
        mRefreshLayout.autoRefresh();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_account;
    }

    @Override
    protected void initView() {
        mRefreshLayout = findViewById(R.id.smart_refresh);
        mBackIm = findViewById(R.id.account_back_im);
        mAddBankTv = findViewById(R.id.account_add_bank_tv);
        mBankAdp = new AccountBankAdp(this);
        mRecy = findViewById(R.id.account_bank_recy);
        mRecy.setLayoutManager(new LinearLayoutManager(this));
        mRecy.setAdapter(mBankAdp);
        ListViewUtils.setDefaultEmpty(mRecy);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mAddBankTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(UserBindBankActivity.class);
            }
        });
        mBankAdp.setAccountBankAdpListener(new AccountBankAdp.AccountBankAdpListener() {
            @Override
            public void onIitemChanged(ResMyBankListEntity.DataBean entity) {
                UserBindBankActivity.start(UserAccountActivity.this,entity.getId());
            }
        });
        mRefreshLayout.setOnRefreshListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                mPresenter.getBankList(ship,take);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mRefreshLayout.setEnableLoadMore(true);
                ship = 0;
                mPresenter.getBankList(ship,take);
            }
        });
    }

    @Override
    protected UserAccountContract.Presenter createPresenter() {
        return new UserAccountPresenter(this);
    }

    @Override
    public void setBankList(ResMyBankListEntity mData) {
        if(mData != null){
            if(mRefreshLayout.isLoading()){
                mBankAdp.addData(mData.getData());
            }else if(mRefreshLayout.isRefreshing()){
                mBankAdp.setData(mData.getData());
            }else{
                mBankAdp.setData(mData.getData());
            }
            ship = mBankAdp.getItemCount();
            if(mData.getCount().equals("0")){
                mRefreshLayout.setEnableLoadMore(false);
            }else{
                mRefreshLayout.setEnableLoadMore(true);
            }
        }
    }

    @Override
    public void setPresenter(UserAccountContract.Presenter presenter) {

    }

    @Override
    public void dismissLoadingDialog() {
        super.dismissLoadingDialog();
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
    }
}
